﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eShop.Model;
using eShop.Business;

public partial class controls_address : System.Web.UI.UserControl
{
    public event AddressChangedHandler OnAddressChanged;

    public delegate void AddressChangedHandler(object sender, Address address);

    
    private Address _address;
    public Address DataSource
    {
        get
        {
            return _address;
        }
        set
        {
            _address = value;
        }
    }

    public void DataRead()
    {
        _address = new Address();
        _address.Name = tbName.Text.Trim();
        _address.PhoneNumber = tbTelephoneNumber.Text.Trim();
        _address.AddressLine1 = tbLine1.Text.Trim();
        _address.AddressLine2 = tbLine2.Text.Trim();
        _address.Zip = tbZipCode.Text.Trim();

        _address.City = ddlCity.SelectedItem.Text;
        _address.State = ddlState.SelectedItem.Text;
        _address.Country = ddlCountry.SelectedItem.Text;
    }

    public void DataBind()
    {
        if (_address == null)
        {
            BindCountry();
            BindStateByCountry(Convert.ToInt32(ddlCountry.SelectedValue));
            BindCityByState(Convert.ToInt32(ddlState.SelectedValue));
            tbName.Text = "";
            tbTelephoneNumber.Text = "";
            tbLine1.Text = "";
            tbLine2.Text = "";
            tbZipCode.Text = "";
        }
        else
        {
            BindCountry();
            ListItem countryItem = ddlCountry.Items.FindByText(_address.Country);
            if (countryItem != null)
            {
                countryItem.Selected = true;
                BindStateByCountry(Convert.ToInt32(ddlCountry.SelectedValue));

                ListItem stateItem = ddlState.Items.FindByText(_address.State);
                if (stateItem != null)
                {
                    stateItem.Selected = true;
                    BindCityByState(Convert.ToInt32(ddlState.SelectedValue));

                    ListItem cityItem = ddlCity.Items.FindByText(_address.City);
                    if (cityItem != null)
                    {
                        cityItem.Selected = true;
                    }
                }
            }

            tbName.Text = _address.Name;
            tbTelephoneNumber.Text = _address.PhoneNumber;
            tbLine1.Text = _address.AddressLine1;
            tbLine2.Text = _address.AddressLine2;
            tbZipCode.Text = _address.Zip;
        }
    }

    private void BindCountry()
    {
        ddlCountry.DataSource = MetaDataManager.GetAllCountries();
        ddlCountry.DataBind();
        ddlCountry.SelectedIndex = 0;
    }

    private void BindStateByCountry(int countryId)
    {
        ddlState.DataSource = MetaDataManager.GetAllStatesByCountry(countryId);
        ddlState.DataBind();
        ddlState.SelectedIndex = 0;
    }
    
    private void BindCityByState(int stateId)
    {
        ddlCity.DataSource = MetaDataManager.GetAllCitiesByState(stateId);
        ddlCity.DataBind();
        ddlCity.SelectedIndex = 0;
        AddressChangedTrigger();
    }

    private void AddressChangedTrigger()
    {
        if (OnAddressChanged != null)
        {
            DataRead();
            OnAddressChanged(this, _address);
        }
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindStateByCountry(Convert.ToInt32(ddlCountry.SelectedValue));
        BindCityByState(Convert.ToInt32(ddlState.SelectedValue));
    }

    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCityByState(Convert.ToInt32(ddlState.SelectedValue));
    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        AddressChangedTrigger();
    }
}